package com.google.code.jmarias.server.tcp;

import com.google.code.jmarias.common.NewTable;
import com.google.code.jmarias.common.Token;
import com.google.code.jmarias.common.action.AbstractSignedMessage;
import com.google.code.jmarias.common.action.ChooseSuitAction;
import com.google.code.jmarias.common.action.GameAction;
import com.google.code.jmarias.common.action.SignedAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Ondrej Michal
 */
public class BotContactableUser extends AbstractContactableUser {

    private static Log log = LogFactory.getLog(BotContactableUser.class);

    // @TODO sjednotit komunikaci remote a local clienta
    private LocalServer server;

    private NewTable table = new NewTable();

    private Room room;

    public BotContactableUser(Room room) {
        this.room = room;
    }

    public Room getRoom() {
        return room;
    }

    public LocalServer getServer() {
        return server;
    }

    public void setServer(LocalServer server) {
        this.server = server;
    }

    @Override
    public void sendMessage(AbstractSignedMessage message) {
        if (message instanceof SignedAction) {
            SignedAction signedAction = (SignedAction) message;
            if (signedAction.getRawAction() instanceof GameAction) {
                signedAction.updateTable(table);
//                log.debug("Novy stav stolu:\n" + table);
            }
        } else if (message instanceof Token) {
            Token token = (Token) message;
            if (token.getPlayingNow().getUsername().equals(getUsername())) {
                log.debug(this + ": Ceka se na me, ted se ukaze, co umim vymyslet");
                if (token.getAllowedActions().contains(ChooseSuitAction.class)) {
                    server.sendAction(new ChooseSuitAction());
                }
//                        if (token.getAllowedActions().contains(PickedTrumpAction.class)) {
//                            server.sendAction(new PickedTrumpAction(table.getPlayer(0).getHand().get(0)));
//                        }

            }
        }
    }

    @Override
    public String toString() {
        return "bot " + getUsername();
    }

}
